<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use chrome\ChromePhp;
use common\Common;

//引入model
use app\common\model\LibSlide;
use uploadImg\UtilImage;


/**
 * 后台幻灯片管理操作控制器
 * @name BaseSlide.php
 * @package yybld
 * @category controller
 * @link
 * @author bsy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-06-01
 */
class BaseSlide extends AdminController
{
    protected $LibSlide;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->LibSlide = new LibSlide();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 幻灯片列表页
     */
    function slideList()
    {
        $this->getSetMenu($this);
        $type = input('type');
        $condition = [];
        if ($type) {
            $condition['type'] = $type;
        }
        $this->assign('type', $type);
        $slideList = $this->LibSlide->findAllSlide($condition);
        $this->assign('slideList', $slideList['data']);
        $this->log(__CLASS__, __FUNCTION__, "幻灯片列表页", 1, 'view');
        return $this->fetch('Slide/slideList/slideList');
    }


    /**
     * 删除幻灯片信息
     */
    function delSlide()
    {
        $ids = input("param.ids");
        $findRes = $this->LibSlide->findSlide(['id' => $ids]);
        if ($findRes['errorCode'] == 0) {
            $result = $this->LibSlide->deleteSlide(array("id" => array("in", $ids)));
            $findRes['data']['img_url'] && @unlink(ROOT_PATH . 'public/' . str_replace('./','',$findRes['data']['img_url']));
            $findRes['data']['thumb'] && @unlink(ROOT_PATH . 'public/' . str_replace('./','',$findRes['data']['thumb']));
            echo json_encode($result);
        }
    }


    /**
     * 添加幻灯片页面
     */
    function addSlide()
    {
        $this->getMenu($this);
        $type = input("type");
        $this->assign('type', $type);
        $this->log(__CLASS__, __FUNCTION__, "添加幻灯片页面", 1, 'view');
        return $this->fetch('Slide/addSlide/addSlide');
    }


    /**
     * 编辑幻灯片页面
     */
    function editSlide()
    {
        $this->getMenu($this);
        $slideInfo = $this->LibSlide->findSlide(['id' => input("param.id")]);
        $this->assign('slideInfo', $slideInfo['data']);
        $this->log(__CLASS__, __FUNCTION__, "编辑幻灯片页面", 1, 'view');
        return $this->fetch('Slide/editSlide/editSlide');
    }

    /**
     * 添加幻灯片
     */
    function insertSlide()
    {
        $slideInfo = $this->getArgsList($this, array('name', 'url', 'sort', 'target', 'type', 'content'));
        $resultImg = UtilImage::uploadPhoto('imgurl', "upload/image/slide", 86.4, 48);
        if ($resultImg) {
            $slideInfo['img_url'] = $resultImg['url'];
            $slideInfo['thumb'] = $resultImg['thumb'];
            $result = $this->LibSlide->addSlide($slideInfo);
            $this->log(__CLASS__, __FUNCTION__, "添加幻灯片", 0, 'add');
            echo json_encode($result);
        } else {
            echo json_encode(Common::errorArray(1, "上传图片失败", 0));
        }
    }

    /**
     * 更新幻灯片信息
     */
    function updateSlide()
    {
        $slideInfo = $this->getArgsList($this, array('name', 'url', 'sort', 'target', 'content'));
        if (input('param.imgFlag') == 1) {//判断是否需要重新上传图片
            //更新图片
            $prevUrl = input('param.prevurl');
            if ($prevUrl) {
                $resultImg = UtilImage::uploadPhoto('imgurl', "upload/image/slide", 86.4, 48);
                if ($resultImg) {
                    $slideInfo['img_url'] = str_replace('./','',$resultImg['url']);
                    $slideInfo['thumb'] = str_replace('./','',$resultImg['thumb']);
                    $prevThumbUrl = substr($prevUrl, 0, strripos($prevUrl, '.')) . "_thumb" . substr($prevUrl, strripos($prevUrl, '.'));
                    @unlink(ROOT_PATH . 'public/' . str_replace('./', '', $prevUrl));//delete url
                    @unlink(ROOT_PATH . 'public' . $prevThumbUrl);//delete thumb
                } else {
                    echo json_encode(Common::errorArray(1, "修改图片失败", 0));
                }
            } else {
                echo json_encode(Common::errorArray(1, "原图url未传，不能删除", 0));
            }
        }
        $result = $this->LibSlide->updateSlide(array("id" => input("id")), $slideInfo);
        $this->log(__CLASS__, __FUNCTION__, "更新幻灯片信息", 0, 'update');
        echo json_encode($result);
    }
}

?>